package com.yongjie.springbootlogin.controller;


import com.yongjie.springbootlogin.entity.DoctorInfo;
import com.yongjie.springbootlogin.entity.Login;
import com.yongjie.springbootlogin.entity.PesonInfo;
import com.yongjie.springbootlogin.entity.Registration;
import com.yongjie.springbootlogin.service.LoginService;
import com.yongjie.springbootlogin.service.PesonInfoService;
import com.yongjie.springbootlogin.service.RegistrationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    /**
     * 注入service
     */
    @Autowired
    private LoginService userLoginService;
    @Autowired
    private RegistrationServiceImpl Rservice;
    @Autowired
    private registrationController r;
    @Autowired
    PesonInfoService pesonInfoService;

    /**
     * 跳转到登录登录页面
     */
    @RequestMapping(value = {"/login"})
    public String loginHtml() {
        return "userLogin";
    }

    /**
     * 跳转到用户注册页面
     */
    @RequestMapping(value = {"/registerpage"})
    public String registerpage() {
        return "register";
    }

    /**
     * 获取用户与登录密码，用户登录
     */

    @PostMapping(value = {"/userLogin"})
    public String userLogin(Map<String, Object> map, @RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpServletRequest request, HttpSession session,
                            HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");


        if (StringUtils.isEmpty(username)) {
            response.getWriter().write("<script>  alert('用户名不能为空')</script>");
            return "userLogin";
        }

        if (StringUtils.isEmpty(password)) {
            response.getWriter().write("<script>  alert('密码不能为空')</script>");
            return "userLogin";
        }

        Login user = userLoginService.findByuser(username, password);
        /**
         *  判断用户限权
         */
        if (user != null) {
            //用户限权
            if (user.getRoc().equals("3")) {
                session.setAttribute("username", username);
                session.setAttribute("roc", user.getRoc());
                /**
                 * 获取挂号记录
                 */
//            List<Registration> rGlist=  Rservice.ListRegistration();
//            System.out.println(rGlist);
//            map.put("rts",rGlist);

                return r.clientAllList(map, request);
            }
            //管理员限权
            if (user.getRoc().equals("1")) {
                session.setAttribute("username", username);
                session.setAttribute("roc", user.getRoc());
                return "redirect:/main.html";
            }
            //医生限权
            if (user.getRoc().equals("2")) {
                session.setAttribute("username", username);
                session.setAttribute("userID", user.getId());
                session.setAttribute("roc", user.getRoc());
                return "clock";
            }

            //登录成功
            //将用户信息放入session  用于后续的拦截器
//            request.getSession().setAttribute("session_user",user);
            //将用户信息放入session  用于后续的拦截器
            session.setAttribute("username", username);
            //重定向 redirect: 可以重定向到任意一个请求中，地址栏改变,防止重新提交表单
            return "redirect:/main.html";
        }
        response.getWriter().write("<script>  alert('用户名和密码错误')</script>");
        return "userLogin";
    }

    /**
     * 注册新用户
     *
     * @param username
     * @param password
     * @param password2
     * @param age
     * @return
     */

    @RequestMapping(value = {"/usergister"})
    public String adduser(Map<String, Object> map, @RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("password2") String password2,
                          @RequestParam("age") int age) {
        //判断用户名是否存在
        Boolean b = userLoginService.finusernameByusername(username);
        if (b) {
            if (StringUtils.isEmpty(username)) {
                map.put("masg", "用户名不能为空");
                return "register";
            }

            if (StringUtils.isEmpty(password)) {
                map.put("masg", "密码不能为空");
                return "register";
            }

            if (StringUtils.isEmpty(password2)) {
                map.put("masg", "确认密码不能为空");
                return "register";
            }

            if (!password.equals(password2)) {
                map.put("masg", "两次密码不相同，注册失败！！");
                return "register";
            } else {
                int res = userLoginService.adduser(username, password, age);
                if (res == 0) {
                    map.put("masg", "注册失败！！");
                    return "register！";
                } else {
                    map.put("masg", "注册成功！！");
                    return "userLogin";
                }
            }
        }else{
            map.put("masg", "用户已存在");
            return "register";
        }

    }

    /**
     * 跳转到主页
     *
     * @return
     */
    @RequestMapping(value = {"/index"})
    public String text(HttpSession session) {
        //重定向 redirect: 可以重定向到任意一个请求中，地址栏改变
        return "redirect:/main.html";
    }

    /**
     * 登出
     *
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        //清空session里面的用户信息
        session.removeAttribute("username");
        //注销session
        session.invalidate();
        return "userLogin";
    }


}
